package com.qin.malldiscount.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qin.mall.common.api.CommonResult;
import com.qin.mall.common.api.MyBatisPlusPage;
import com.qin.malldiscount.domain.SmsCoupon;
import com.qin.malldiscount.dto.SmsCouponParam;
import com.qin.malldiscount.service.SmsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 优惠券管理Controller
 * @author Leisure
 * create 2022 2022/11/24 12:20
 **/
@RestController
@RequestMapping("/couponManage")
public class SmsCouponController {

    @Autowired
    private SmsCouponService couponService;

    /**
     * 添加优惠券
     */
    @PostMapping("/create")
    @ResponseBody
    public CommonResult add(@RequestBody SmsCouponParam couponParam) {
        int count = couponService.create(couponParam);
        if(count>0){
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }


    /**
     * 删除优惠券
     */
    @PostMapping("/delete/{id}")
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        int count = couponService.delete(id);
        if(count>0){
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 修改优惠券
     */
    @PostMapping("/update/{id}")
    @ResponseBody
    public CommonResult update(@PathVariable Long id,@RequestBody SmsCouponParam couponParam) {
        int count = couponService.update(id,couponParam);
        if(count>0){
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 根据优惠券名称和类型分页获取优惠券列表
     */
    @GetMapping("/list")
    @ResponseBody
    public CommonResult<MyBatisPlusPage<SmsCoupon>> list(
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "type",required = false) Integer type,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<SmsCoupon> couponList = couponService.list(name,type,pageSize,pageNum);
        return CommonResult.success(MyBatisPlusPage.mpPage(couponList));
    }

    /**
     * 获取单个优惠券的详细信息
     */
    @GetMapping("/{id}")
    @ResponseBody
    public CommonResult<SmsCouponParam> getItem(@PathVariable Long id) {
        SmsCouponParam couponParam = couponService.getItem(id);
        return CommonResult.success(couponParam);
    }

}
